package com.uphone.kingmall.main;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.china.zhuoyue.R;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.uphone.kingmall.activity.HomeCatListActivity;
import com.uphone.kingmall.activity.SearchGoodsListActivity;
import com.uphone.kingmall.adapter.ClassHomeAdapter;
import com.uphone.kingmall.adapter.MenuAdapter;
import com.uphone.kingmall.base.BaseFragment;
import com.uphone.kingmall.bean.CatsListBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.ConstansUtils;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.intent.IntentUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * author : SuMeng
 * e-mail : 986335838@qq.com
 * time   : 2019/02/15
 * desc   : 分类fragment
 */
public class ClassFragment extends BaseFragment {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_sousuo)
    ImageView ivSousuo;
    @BindView(R.id.rl_sousuo)
    RelativeLayout rlSousuo;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.lv_menu)
    ListView lvMenu;
    @BindView(R.id.lv_home)
    GridView lvHome;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;
    private ClassHomeAdapter homeAdapter;
    private MenuAdapter menuAdapter;
    private int currentItem;
    private List<Integer> showTitle = new ArrayList<>();
    private ArrayList<CatsListBean.CatsBean> catsList;
    private List<CatsListBean.CatsBean.ChildrenBean> catsBean;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_cricle_more;
    }

    @Override
    public void onResume() {
        super.onResume();
//        loadData();
    }

    @Override
    public void initData() {
        homeAdapter = new ClassHomeAdapter(getActivity());
        lvHome.setAdapter(homeAdapter);
        menuAdapter = new MenuAdapter(getActivitySafely(), catsList);
        lvMenu.setAdapter(menuAdapter);
        lvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                menuAdapter.setSelectItem(position);
                menuAdapter.notifyDataSetInvalidated();
//                lvHome.setSelection(showTitle.get(position));
                loadHomeData(catsList.get(position).getChildren());
            }
        });

        lvHome.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (catsBean != null && catsBean.size() > position) {
                    CatsListBean.CatsBean.ChildrenBean bean = catsBean.get(position);
                    IntentUtils.getInstance().with(getActivity(), HomeCatListActivity.class)
                            .putInt(ConstansUtils.ID, bean.getId())
                            .putString(ConstansUtils.TITLE, bean.getCatName())
                            .putString(ConstansUtils.URL, MyUrl.getCatGoods).start();
                }
            }
        });
        loadData();
    }

    private void loadHomeData(List<CatsListBean.CatsBean.ChildrenBean> catsBean) {
        this.catsBean = catsBean;
        homeAdapter.setList(catsBean);
    }

    private void loadData() {
        OkGoUtils.normalRequest(MyUrl.getCatsList, null, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                final CatsListBean catsListBean = GsonUtils.getGson().fromJson(body, CatsListBean.class);
                if (catsListBean.getCode() == 0) {
                    if (catsList == null) {
                        catsList = new ArrayList<>();
                    }
                    catsList.clear();
                    showTitle.clear();
                    if (catsListBean.getCats() != null) {
                        catsList.addAll(catsListBean.getCats());
                        for (int i = 0; i < catsListBean.getCats().size(); i++) {
                            showTitle.add(i);
                        }
                    }
                    if (catsList != null && catsList.size() != 0) {
                        loadHomeData(catsList.get(0).getChildren());
                        menuAdapter.setList(catsList);
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {
            }
        });
    }

    @Override
    public void initView(View view) {
        tvSearch.setText("请输入您要搜索的内容");
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableRefresh(false);

    }

    @OnClick({R.id.iv_sousuo, R.id.rl_sousuo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_sousuo:
                break;
            case R.id.rl_sousuo:
                startActivity(new Intent(getActivity(), SearchGoodsListActivity.class));
                break;
        }
    }
}
